<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<link href="style.css" type="text/css" rel="stylesheet">
<title>AAA—ASCII Adjust After Addition </title></head>
<body>
<h1>AAA—ASCII Adjust After Addition</h1>
<table>
<tr>
<th>Opcode</th>
<th>Instruction</th>
<th>Op/En</th>
<th>64-bit Mode</th>
<th>Compat/Leg Mode</th>
<th>Description</th></tr>
<tr>
<td>37</td>
<td>AAA</td>
<td>NP</td>
<td>Invalid</td>
<td>Valid</td>
<td>ASCII adjust AL after addition.</td></tr></table>
<h3>Instruction Operand Encoding</h3>
<table>
<tr>
<td>Op/En</td>
<td>Operand 1</td>
<td>Operand 2</td>
<td>Operand 3</td>
<td>Operand 4</td></tr>
<tr>
<td>NP</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td></tr></table>
<h2>Description</h2>
<p>Adjusts the sum of two unpacked BCD values to create an unpacked BCD result. The AL register is the implied source and destination operand for this instruction. The AAA instruction is only useful when it follows an ADD instruction that adds (binary addition) two unpacked BCD values and stores a byte result in the AL register. The AAA instruction then adjusts the contents of the AL register to contain the correct 1-digit unpacked BCD result.</p>
<p>If the addition produces a decimal carry, the AH register increments by 1, and the CF and AF flags are set. If there was no decimal carry, the CF and AF flags are cleared and the AH register is unchanged. In either case, bits 4 through 7 of the AL register are set to 0.</p>
<p>This instruction executes as described in compatibility mode and legacy mode. It is not valid in 64-bit mode.</p>
<h2>Operation</h2>
<pre>IF 64-Bit Mode
    THEN
         #UD;
    ELSE
         IF ((AL AND 0FH) &gt; 9) or (AF = 1)
              THEN
                    AX ← AX + 106H;
                    AF ← 1;
                    CF ← 1;
              ELSE
                    AF ← 0;
                    CF ← 0;
         FI;
         AL ← AL AND 0FH;
FI;</pre>
<h2>Flags Affected</h2>
<p>The AF and CF flags are set to 1 if the adjustment results in a decimal carry; otherwise they are set to 0. The OF, SF, ZF, and PF flags are undefined.</p>
<h2>Protected Mode Exceptions</h2>
<table class="exception-table">
<tr>
<td>#UD</td>
<td>If the LOCK prefix is used.</td></tr></table>
<h2>Real-Address Mode Exceptions</h2>
<p>Same exceptions as protected mode.</p>
<h2>Virtual-8086 Mode Exceptions</h2>
<p>Same exceptions as protected mode.</p>
<h2>Compatibility Mode Exceptions</h2>
<p>Same exceptions as protected mode.</p>
<h2>64-Bit Mode Exceptions</h2>
<table class="exception-table">
<tr>
<td>#UD</td>
<td>If in 64-bit mode.</td></tr></table></body></html>